package org.isource.admin;

import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.isource.common.PMF;
import org.isource.pojo.ThirdParty;

@SuppressWarnings("serial")
public class DeleteThirdParty extends HttpServlet {

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		String key = req.getParameter("key");
		if (key == null) {
			resp.getWriter().println("No key provided.");
		}

		doPostJDO(Long.valueOf(key));

		resp.sendRedirect("/admin");
	}

	private void doPostJDO(long key) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			ThirdParty tp = pm.getObjectById(ThirdParty.class, key);
			pm.deletePersistent(tp);
		} finally {
			if (pm.currentTransaction().isActive()) {
				pm.currentTransaction().rollback();
			}
			pm.close();
		}
	}
}
